Both the List.Find
method and the Enumerable.FirstOrDefault
method can be used to locate the first element that meets a
specified condition within a collection. However, for List
objects, List.Find
may offer superior performance compared to
Enumerable.FirstOrDefault
. While the performance difference might be negligible for small collections, it can become significant for
larger collections. This observation also holds true for ImmutableList
and arrays.
It is important to enable this rule with caution, as performance outcomes can vary significantly across different runtimes. Notably, the performance improvements in .NET 9 have brought
FirstOrDefault
closer to the performance of collection-specific Find
methods in most scenarios.
Applies to
What is the potential impact?
We measured at least 2x improvement in the execution time. For more details see the Benchmarks
section from the More info
tab.